﻿using System;
using System.Collections.Generic;
using System.Text;

namespace VoteUphold
{
    /// <summary>
    /// 获取页面 数据
    /// </summary>
    public class GetViewData
    {
        private const string __VIEWSTATE = "id=\"__VIEWSTATE\" value=\"";
        private const string __EVENTVALIDATION = "id=\"__EVENTVALIDATION\" value=\"";

        private static GetViewData getViewData = null;
        /// <summary>
        /// 获取 对象 (懒汉式) 
        /// </summary>
        /// <returns></returns>
        public static GetViewData GetInstance()
        {
            //synchronized         
            if (getViewData == null)
            {
                getViewData = new GetViewData();
            }
            return getViewData;
        }

        #region  获取页面的 VeiwState 及EventValidation
        /// <summary>
        /// 获取页面的 VeiwState 及EventValidation
        /// </summary>
        /// <param name="srcString"></param>
        public VE__VIEWSTATE__EVENTVALIDATION Get__VIEWSTATE__EVENTVALIDATION(string htmlStr)
        {
            return new VE__VIEWSTATE__EVENTVALIDATION() { __VIEWSTATE = GetData(htmlStr, __VIEWSTATE, "\""), __EVENTVALIDATION = GetData(htmlStr, __EVENTVALIDATION, "\"") };
        }
        #endregion

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="htmlStr">需要查找的数据源</param>
        /// <param name="startStr">起始位置字符串</param>
        /// <param name="endStr">结束位置字符串</param>
        /// <param name="startIndex">起始位置的下标</param>
        /// <returns></returns>
        public string GetData(string htmlStr, string startStr,string endStr,int startIndex=0)
        {
            string result = "";
            if (startIndex<0)
            {
                startIndex = 0;
            }
            int i = htmlStr.IndexOf(startStr,startIndex);
            if (i <= -1) return result;
            i += startStr.Length;
            int j = htmlStr.IndexOf(endStr, i);
            if (j <= -1) return result;
            result = htmlStr.Substring(i, j - i);
            return result;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="htmlStr">需要查找的数据源</param>
        /// <param name="startStr">起始位置字符串</param>
        /// <param name="endStr">结束位置字符串</param>
        ///<param name="startIndexOut">返回起始位置</param>
        /// <param name="startIndex">起始位置的下标</param>
        /// <returns></returns>
        private string GetData(string htmlStr, string startStr, string endStr, out int startStrIndexOut, int startIndex = 0)
        {
            string result = "";
            startStrIndexOut = 0;
            if (startIndex < 0)
            {
                startIndex = 0;
            }
            int i = htmlStr.IndexOf(startStr, startIndex);
            if (i <= -1) return result;
            i += startStr.Length;
            startStrIndexOut = i;
            int j = htmlStr.IndexOf(endStr, i);
            if (j <= -1) return result;
            result = htmlStr.Substring(i, j - i);
            return result;
        }
    }
}
